package com.elitel.frame.business.controller;

import com.elitel.common.minio.MinioTemplate;
import com.elitel.frame.base.service.CfgFileParamService;
import com.elitel.frame.base.service.JdpCommFileService;
import com.elitel.frame.base.service.SysPrjServiceService;
import com.elitel.frame.main.entity.JdpCommFile;
import com.elitel.frame.main.entity.vo.CfgFileParamVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 文件服务
 */
@Controller
@RequestMapping(value = "/business/fileServer")
@Api(value = "/business/fileServer", tags = "聚合服务接口")
public class FileServerController {
    protected final Logger log =LoggerFactory.getLogger(this.getClass());

    @Resource
    JdpCommFileService jdpCommFileService;
    @Resource
    private SysPrjServiceService sysPrjServiceService;
    @Resource
    CfgFileParamService cfgFileParamService;
    @Resource
    MinioTemplate minioTemplate;
    @Value("${minio.bucket}")
    private String bucketName;


    @Value("${auth.server-name}")
    private String webPageUrl;

    /**
     * 查询文件服务
     * @param request
     * @param response
     * @param serviceID
     * @return
     */
    @ApiOperation(value = "查询文件服务")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "serviceID", value = "服务编码", required = true, dataType = "String", paramType = "query")
    })
    @RequestMapping(value = "/queryFileServer", method = {RequestMethod.POST, RequestMethod.GET})
    public void queryFileServer(HttpServletRequest request, HttpServletResponse response, @RequestParam(value = "serviceID") String serviceID) {
        CfgFileParamVo cfgFileParamVo = cfgFileParamService.getCfgInfo(sysPrjServiceService.getByServiceCode(serviceID).getGuid());
        download(cfgFileParamVo.getFileParamId(), request, response);
    }

    /**
     * 下载文件
     * @param fileId
     * @param request
     * @param response
     */
    @RequestMapping(value = {"/download"}, method = {RequestMethod.GET, RequestMethod.POST})
    public void download(String fileId, HttpServletRequest request, HttpServletResponse response) {
        try {
            JdpCommFile file = jdpCommFileService.queryById(fileId);
            if (file != null) {
                String objectName = file.getFilePath();
                minioTemplate.download(bucketName, objectName, file.getFileName() + "." + file.getFileSuffix(), request, response);
            } else {
                response.sendRedirect(webPageUrl + "/#/fileNotFound");
                log.error("fileId: " + fileId + "不存在");
            }
        } catch (Exception e) {
            try {
                response.sendRedirect(webPageUrl + "/#/fileNotFound");
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
            log.error("minio 下载异常！", e);
        }
    }

}
